# A valid snippet should starts with:
#
#		snippet trigger_word [ "description" [ options ] ]
#
# and end with:
#
#		endsnippet
#
# Snippet options:
#
#		b - Beginning of line.
#		i - In-word expansion.
#		w - Word boundary.
#		r - Regular expression
#		e - Custom context snippet
#		A - Snippet will be triggered automatically, when condition matches.
#
# Basic example:
#
#		snippet emitter "emitter properties" b
#		private readonly ${1} = new Emitter<$2>()
#		public readonly ${1/^_(.*)/$1/}: Event<$2> = this.$1.event
#		endsnippet
#
# Online reference: https://github.com/SirVer/ultisnips/blob/master/doc/UltiSnips.txt


snippet make "makefile templet"

CC = gcc

CFLGAS += -std=c99
CFLAGS += -D TEST
CFLAGS += -g

OBJ_PATH = .
BIN_PATH = .
SRC_PATH = .

IDIR = ./head
INC += -I$(IDIR)

TARGET = $(BIN_PATH)/main 
# ifeq ($(OS),Windows_NT)
# 	TARGET_NAME := $(addsuffix .exe,$(TARGET_NAME))
# endif

C_SRCS += ./main.c
C_SRCS += ./head/list.c
OBJ := $(patsubst %.c,%.o,$(filter %.c,$(C_SRCS)))


$(TARGET) : $(OBJ_PATH)/$(OBJ)
	$(CC) $(INC) $(CFLAGS)  $(OBJ)  -o $(TARGET)
	# @find . -name "*.o"  | xargs rm -f
	# @-rm *.exe
	./$(TARGET)

$(OBJ): %.o : %.c
	$(CC) $(INC) $(CFLAGS) -c $< -o $@

.PHONY : clean
clean:
	@-rm *.exe
	@-rm *.o
endsnippet


snippet make1 "another template" b
CC = gcc
TARGET = build/test
INC_DIRS = -Isrc -I$(UNITY_HOME)
SYMBOLS = -DTEST

UNITY_HOME = /home/agu/gitpool/Unity/src

SRC_FILES = $(UNITY_HOME)/unity.c src/ProductionCode.c test/TestProductionCode.c test/TestProductionCode_Runner.c

CLEANUP = rm -fr build/

all: clean default

default:
	mkdir -p build
	$(CC) $(INC_DIRS) $(SYMBOLS) $(SRC_FILES) -o $(TARGET)
	./$(TARGET)

clean:
	$(CLEANUP)
endsnippet

snippet make_asm "asm make template" b
CC = as 
SYMBOLS += -gstabs
TARGET = a.out
SRC_FILES = cpuid.s

all: default

default:
	@ $(CC) $(SYMBOLS) -o tmp.o $(SRC_FILES) 
	@ ld -o $(TARGET) tmp.o

.PHONY : clean
clean:
	rm *.o
endsnippet


snippet make_gtk "gtk program template" b
# change application name here (executable output name)
TARGET=template_app

# compiler
CC=gcc
# debug
DEBUG=-g
# optimisation
OPT=-O0
# warnings
WARN=-Wall

PTHREAD=-pthread

CCFLAGS=$(DEBUG) $(OPT) $(WARN) $(PTHREAD) -pipe

GTKLIB=`pkg-config --cflags --libs gtk+-3.0`

# linker
LD=gcc
LDFLAGS=$(PTHREAD) $(GTKLIB) -export-dynamic

OBJS=    main.o

all: $(OBJS)
    $(LD) -o $(TARGET) $(OBJS) $(LDFLAGS)
    
main.o: src/main.c
    $(CC) -c $(CCFLAGS) src/main.c $(GTKLIB) -o main.o
    
clean:
    rm -f *.o $(TARGET)
endsnippet


snippet makefile_x86 
obj-m += ${1:hello}.o
KERNEL_DIR ?= /lib/modules/$(shell uname -r)/build


all:
	make -C $(KERNEL_DIR) M=$(PWD) modules
clean:
	make -C $(KERNEL_DIR) M=$(PWD) clean
	rm -f *.ko

endsnippet

snippet makefile_arm 
ARCH            ?= arm
CROSS_COMPILE   ?= /opt/Xilinx/Vitis/2021.2/gnu/aarch32/lin/gcc-arm-linux-gnueabi/bin/arm-linux-gnueabihf-

obj-m += ${1:hello}.o

KERNEL_DIR:=/home/wj/study/qemu/linux-xlnx

PWD?=$(shell pwd)

all:
	make -C $(KERNEL_DIR) ARCH=$(ARCH) CROSS_COMPILE=$(CROSS_COMPILE) M=$(PWD) modules

clean:
	make -C $(KERNEL_DIR) M=$(PWD) clean
	rm -f *.ko
endsnippet

snippet makefile_common 
# make ARCH=x86

ifeq ($(ARCH),arm)
	ARCH = arm
	CROSS_COMPILE = /opt/Xilinx/Vitis/2021.2/gnu/aarch32/lin/gcc-arm-linux-gnueabi/bin/arm-linux-gnueabihf-
	KERNEL_DIR := /home/wj/study/qemu/linux-xlnx
else
	ARCH = 
	CROSS_COMPILE = 
	KERNEL_DIR ?= /lib/modules/$(shell uname -r)/build
endif


obj-m += ${1:hello}.o

all:
	make -C $(KERNEL_DIR) ARCH=$(ARCH) CROSS_COMPILE=$(CROSS_COMPILE) M=$(PWD) modules

clean:
	make -C $(KERNEL_DIR) M=$(PWD) clean
	rm -f *.ko
endsnippet
